import os
from app import app, socketio
from app.mqtt_client import connect_mqtt, disconnect_mqtt

# 检查环境变量
if 'DASHSCOPE_API_KEY' not in os.environ:
    print("警告: 未设置DASHSCOPE_API_KEY环境变量，大模型功能可能无法正常工作")

if __name__ == '__main__':
    try:
        # 连接MQTT服务器
        connect_mqtt()
        
        print("启动Web服务器...")
        print("请访问: http://localhost:5000")
        socketio.run(app, host='0.0.0.0', port=5000,
                     debug=True, allow_unsafe_werkzeug=True)
    except KeyboardInterrupt:
        print("正在关闭应用...")
    finally:
        # 断开MQTT连接
        disconnect_mqtt()
        print("应用已关闭")
